<script>
	function showAttachment(id)
	{
		//showAttachmentIndicator();
		$.ajax({'url':'<?=Yii::app()->createUrl('attachment/list')?>/'+id,'type':'get','success':function(data)
		{
			//hideAttachmentIndicator();
			$('#attachment_status').html(data);
		} ,'cache':false});
		return false;
	}
	function showAttachmentByStudent(id)
	{
		$.getJSON('<?=Yii::app()->createUrl('project/getActiveProject')?>/'+id,function(data)
			{
			$.ajax({'url':'<?=Yii::app()->createUrl('attachment/list')?>/'+data['projectID'],'type':'get','success':function(data)
				{
					$('#attachment_status').html(data);
				} ,'cache':false});
			});
		
		return false;
	}
	
	function showAttachmentIndicator()
	{
		var html="<ul style='text-align: center; vertical-align :top; border: none;'><li><img src='<?=Yii::app()->request->baseUrl?>/images/indicator.gif'></ul></li>";
		$('#attachment_status').html(html);
	}
	
	function hideAttachmentIndicator()
	{
		$('#attachment_status').html('');
	}
	
	function loadAttachmentType()
	{
		$.getJSON("<?=Yii::app()->createUrl('attachment/LoadAttachmentType')?>", function(data) {
			//alert(data);
			for(var i =0; i< data.length; i++){
				$('#attachmentId').append(
					$('<option />')
				        .text(data[i].type)
				        .val(data[i].id)
				);
			}
			});
	}
	
	function uploadAttachment(projectId)
	{
		loadAttachmentType();
		$("#projectID").val(projectId);
		$("#dialogUploadAttachment").dialog("open");
	}
	
	function validateInput(){
		var id=$('#attachmentId').val();
		var txt=$('#attachmentId :selected').text();
		//alert('vaidate input . id =' +id+ ", Text = "+txt);
		if(id != null){
			$("#attachTypeID").val(id);
			$("#attachTypeName").val(txt);
			return true;
		}
		else{
			alert("กรุณาเลือก ชนิดเอกสาร");
			return false;
		}
		
	}
	
	function dialogSubmit(){
		if(validateInput() == true){
			$("#formUploadAttachment").submit();
			$("#dialogUploadAttachment").dialog("close");
		}
	}

</script>

	<?php
		// dialog form input date for print หนังสืออนุมัติโครงการ
		$this->beginWidget('zii.widgets.jui.CJuiDialog', array( // the dialog
		    'id'=>'dialogUploadAttachment',
		    'options'=>array(
		        'title'=>'นำเข้าไฟล์เอกสารประเภทต่างๆ',
		        'autoOpen'=>false,
		        'modal'=>true,
		        'width'=>550,
		        'height'=>250,
				'buttons' => array(
						array('text'=>'ตกลง','click'=> 'js:function(){dialogSubmit();}'),
						array('text'=>'ปิด','click'=> 'js:function(){$(this).dialog("close");}'),
				),
		    ),
		));?>
		<div class="divForUploadAttchment">
			<div style="text-align:center; margin-top: 20px;">
				<form action="<?=Yii::app()->createUrl('attachment/upload')?>" method="post" target="attachmentuploads" enctype='multipart/form-data' id="formUploadAttachment" name="formUploadAttachment">
					<input type="hidden" name="projectID" id="projectID"/>
					<input type="hidden" name="attachTypeID" id="attachTypeID"/>
					<input type="hidden" name="attachTypeName" id="attachTypeName"/>
					<table>
						<tr>
							<td width="100"><label>ชนิดเอกสาร</label></td>
							<td><select id="attachmentId" name="attachmentId"></select></td>
						</tr>
						<tr>
							<td><label>เลือกไฟล์เอกสาร</label></td>
							<td><input type="file" name="fileAttachUpload" id="fileAttachUpload"/></td>
						</tr>
					</table>
					
				</form>
			</div>
		</div>
		<iframe name="attachmentuploads" frameborder="0" height="0" width="0"></iframe>
		<?php $this->endWidget();?>
		